package com.wjx.article_sys.controller;

import com.wjx.article_sys.service.impl.AdminServiceImpl;
import com.wjx.article_sys.service.impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/back")
public class BackController {
    @Autowired
    private HttpSession session;
    @Autowired
    private ArticleServiceImpl articleService;
    @Autowired
    private AdminServiceImpl adminService;
    @GetMapping("/index")
    public String index(Model model) {
        if (session.getAttribute("userName") != null && session.getAttribute("userName") != "空") {
            List<Integer> countList = articleService.articleCount();
            model.addAttribute("allArticleCount",countList.get(0));
            model.addAttribute("userArticleCount",countList.get(1));
            return "back/index";
        }
        else
            return "redirect:/tologin?isLoginBack=1";
    }

    @GetMapping("/toEditArticle")
    public String toEditArticle(@RequestParam(name = "isModify", defaultValue = "0") int isModify,
                               @RequestParam(name = "aId", defaultValue = "0") Integer aId,
                               @RequestParam(name = "title", defaultValue = "") String title,
                               @RequestParam(name = "content", defaultValue = "") String content,
                               Model model) {
        model.addAttribute("isModify", isModify);
        model.addAttribute("title",title);
        model.addAttribute("content",content);
        if (isModify == 1) {
            model.addAttribute("aId", aId);
            model.addAttribute("title", title);
            model.addAttribute("content", content);
        }

        return "/back/editArticle";
    }

    @PostMapping("/editArticle")
    @ResponseBody
    public void editArticle(@RequestParam("title") String title,
                            @RequestParam("content") String content,
                            @RequestParam(value = "id",defaultValue = "0") Integer id,
                            @RequestParam(name = "isModify", defaultValue = "0")
                           int isModify, Model model) throws Exception {
        if (isModify == 0){
            articleService.addArticle(title, content);
        }
        else {
            articleService.updateArticleById(id, title, content);
        }
    }

    @GetMapping("/deleteArticle")
    @ResponseBody
    public void deleteArticle(@RequestParam("id") Integer id) throws Exception {
        articleService.deleteArticleById(id);
    }

    @GetMapping("/toPersonInfo")
    public  String toPersonInfo(Model model) {
        String userName = session.getAttribute("userName").toString();
        model.addAttribute("admin",adminService.queryByUsername(userName));
        return "/back/personInfo";
    }
}
